<?php

namespace app\admin\controller;

use app\admin\model\Form1;
use app\admin\model\Form2;
use app\admin\model\Form3;
use app\admin\model\SystemAdmin;
use app\common\controller\AdminController;
use think\App;
use think\facade\Db;

class Index extends AdminController
{

    /**
     * 后台主页
     * @return string
     * @throws \Exception
     */
    public function index()
    {
        return $this->fetch('', [
            'admin' => session('admin'),
        ]);
    }

    /**
     * 后台欢迎页
     * @return string
     * @throws \Exception
     */
    public function welcome()
    {
        // 今日、昨日、历史累积数据
        $count1 = Form1::whereDay('create_time', 'today')->count();
        $count2 = Form2::whereDay('create_time', 'today')->count();
        $count3 = Form3::whereDay('create_time', 'today')->count();
        $count4 = Form1::whereDay('create_time', 'yesterday')->count();
        $count5 = Form2::whereDay('create_time', 'yesterday')->count();
        $count6 = Form3::whereDay('create_time', 'yesterday')->count();
        $count7 = Form1::count();
        $count8 = Form2::count();
        $count9 = Form3::count();

        // 社区接种人数排名
//        $rank = Db::name('form2')
//            ->alias(['form2' => 'f', 'community' => 'c'])
//            ->join('community', 'c.id=f.community')
//            ->field('count(f.id) as num,c.name')
//            ->group('f.community')
//            ->order('num', 'desc')
//            ->select();

        $this->assign([
            'count1'    =>  $count1,
            'count2'    =>  $count2,
            'count3'    =>  $count3,
            'count4'    =>  $count4,
            'count5'    =>  $count5,
            'count6'    =>  $count6,
            'count7'    =>  $count7,
            'count8'    =>  $count8,
            'count9'    =>  $count9,
//            'rank'      =>  $rank,
        ]);
        return $this->fetch();
    }

    /**
     * 修改密码
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editPassword()
    {
        $id = session('admin.id');
        $row = SystemAdmin::withoutField('password')->find($id);
        if ($row->isEmpty()) {
            $this->error('用户信息不存在');
        }
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'password|登录密码'       => 'require',
                'password_again|确认密码' => 'require',
            ];
            $this->validate($post, $rule);
            if ($post['password'] != $post['password_again']) {
                $this->error('两次密码输入不一致');
            }
            try {
                $save = $row->save([
                    'password' => password($post['password']),
                ]);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

}
